﻿using System;
using System.Linq;

namespace ProxyViewModel.Messaging {
	internal class MessageContract : IEquatable<MessageContract> {
		private int _hashCode = 0;
		private readonly Type[] _parametersTypes;
		private readonly string _selector;

		public Type[] ParametersTypes {
			get { return _parametersTypes; }
		}

		public string Selector {
			get { return _selector; }
		}

		public MessageContract(Type[] parametersTypes, string selector) {
			_parametersTypes = parametersTypes;
			_selector = selector;
		}

		public bool Equals(MessageContract other) {
			return other.GetHashCode() == GetHashCode();
		}

		public override bool Equals(object obj) {
			if (!(obj is MessageContract)) {
				return false;
			}
			return Equals(obj as MessageContract);
		}

		public override int GetHashCode() {
			if (_hashCode != 0) {
				return _hashCode;
			}

			string concat = string.Empty;
			for (int i = 0; i < ParametersTypes.Length; ++i) {
				concat = string.Concat(concat, ParametersTypes[i].FullName);
			}
			if (!string.IsNullOrEmpty(Selector)) {
				concat = string.Concat(Selector);
			}
			_hashCode = concat.GetHashCode();
			return _hashCode;
		}
	}
}
